CROSS_COMPILE ?= 
CC ?= $(CROSS_COMPILE)gcc

TARGET ?= main.exe

INCDIRS := add \
		   main \
		   square

SRCDIRS := add \
		   main \
		   square

INCLUDE := $(patsubst %, -I %, $(INCDIRS))

SFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.S))
sFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.s))
CFILES := $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c))

SFILENDIR := $(notdir $(SFILES))
sFILENDIR := $(notdir $(sFILES))
CFILENDIR := $(notdir $(CFILES))

SOBJS := $(patsubst %, obj/%, $(SFILENDIR:.S=.o))
sOBJS := $(patsubst %, obj/%, $(sFILENDIR:.s=.o))
COBJS := $(patsubst %, obj/%, $(CFILENDIR:.c=.o))
OBJS := $(SOBJS) $(sOBJS) $(COBJS)

VPATH := $(SRCDIRS)


$(TARGET): CHECKDIR $(OBJS)
	$(CC) -o $@ $(OBJS)

$(SOBJS): obj/%.o : %.S
	$(CC) -c $(INCLUDE) -o $@ $^

$(sOBJS): obj/%.o : %.s
	$(CC) -c $(INCLUDE) -o $@ $^

$(COBJS): obj/%.o : %.c
	$(CC) -c $(INCLUDE) -o $@ $^

.PHONY: CHECKDIR
CHECKDIR:
	mkdir -p obj

.PHONY: clean
clean:
	rm -f $(TARGET)
	rm -rf obj/

